// 用于创建当前游戏中可使用的玩家类
var pokerPlayer = require('../Poker/player').PokerPlayer;
var Poker = require('../Poker/Poker');
var rules = require('./rules').Rules;
var constVar = require('./const');

exports.XYZjhPokerPlayer = pokerPlayer.extend ({
    _handCards: null,       // 手牌
    _prehandcards: null,

    _cardValue: 0,           // 牌型

    _status:  0,             // 玩家状态
    _isLookCards: false,    // 是否看牌

    _playerBet: 0,           // 玩家押注的数量
    _playerGameTurn: 0,      // 玩家操作轮数

    _playerTurnBet: [],      // 玩家每轮下注内容

    _isAlwaysGen : false,   // 是否跟到底

    _comparePlayers : [],    // 比牌的玩家列表

    /**************************************************
     * 玩法接口
     **************************************************/

    /**
     * 添加押注
     * @param bet
     */
    addBet : function (bet) {

        this._playerBet += bet;

        this._playerTurnBet.push (bet);
    },

    /**
     * 添加玩家轮数
     */
    addGameTurn : function () {
        this._playerGameTurn += 1;
    },

    /**
     * 检查当前轮数是否已经满足要求
     */
    checkTurns : function (round) {

        // 如果为0就是不选择...
        if (round == 0) {
            return true;
        }

        return this._playerGameTurn > round;
    },

    /**
     * 重置房间消息
     */
    reSetRoom : function () {
        this._handCards.reset();

        this._stat.roundScore = 0;
        this._stat.roundCoin = 0;

        this._cardValue = 0;
        this._playerGameTurn = 0;
        this._isLookCards = false;
        this._isAlwaysGen = false;

        this._playerBet = 0;
        this._status = constVar.status.wait;
        this._playerTurnBet = [];

        this._comparePlayers = [];
    },

    /**
     * 添加比牌玩家
     * @param comparePlayer
     */
    addComparePlayer : function (comparePlayer) {

        this._comparePlayers.push (comparePlayer);

    },

    /**
     * 发送已经比牌玩家的牌型
     */
    sendComparePlayerCard : function () {

        if (!this._isLookCards) {
            // 添加当前玩家的牌型
            this.sendPlayerCard();
        }

        // 根据当前比牌玩家数进行获取
        for (var index = 0; index < this._comparePlayers.length; ++index) {

            var seat = this._comparePlayers[index ].getIndex();
            var card = this._comparePlayers[index ].getHandCards().getInitCards();

            this.send("onPlayerCard", {
                playerIndex : seat,
                cards : card,
                cardValue : this._comparePlayers[index ]._cardValue
            });
        }
    },

    /**
     * 检查当前玩家是否是有效玩家
     * @returns {boolean}
     */
    isAvaliable : function () {

        if (this._status != constVar.status.abandon
            && this._status != constVar.status.fail) {
            return true;
        }

        logger.debug ("玩家:" + this.getIndex() + "已经无法行动");

        return false;
    },

    /**
     * 重置玩家状态
     */
    resetOperaStatus : function () {
        this._status = constVar.status.wait;
    },

    /**
     * 设置玩家的状态
     * @param status
     */
    setPlayerStatus : function (status) {
        this._status = status;
    },

    /**
     * 检查玩家是否可以进行操作
     * @returns {boolean}
     */
    isCanOpertor : function () {

        return this._status == constVar.status.wait;
    },

    /**
     * 添加失败局数
     */
    addFailRound : function () {

        if (isNaN(this._stat._failRound)) {
            this._stat._failRound = 1;
        }else {
            this._stat._failRound += 1;
        }
    },

    /**
     * 添加胜利局数
     */
    addWinRound : function () {

        if (isNaN(this._stat._winRound)) {
            this._stat._winRound = 1;
        }else {
            this._stat._winRound += 1;
        }

    },

    /**
     * 分析玩家牌型
     * @param rule
     */
    analysis : function (rule) {
        var cards = this._handCards.getInitCards();
        this._cardValue = rules.analysis(cards, rule);
    },

    /**
     * 设置单局最大分数
     */
    addMaxWinScore : function () {

        if (isNaN(this._stat._maxWinScore)) {
            this._stat._maxWinScore = this._playerBet;
        }else {

            if (this._stat._maxWinScore < this._playerBet) {
                this._stat._maxWinScore = this._playerBet;
            }
        }
    },

    /**
     * 添加最大的牌型
     */
    addMaxCardValue : function () {

        if (this._stat._maxCardValue) {
            var maxCardValue = this._stat._maxCardValue;
            var result = rules.compareCardValue(this._cardValue, maxCardValue);

            if (result != -1) {
                this._stat._maxCardValue = result;
            }
        }else {
            this._stat._maxCardValue = this._cardValue;
        }
    },

    /**
     * 发送并且初始化当前玩家的手牌
     * @param cardArr
     */
    InitHandCards: function (cardArr) {
        this._handCards.init(cardArr);
    },

    /**
     * 发送当前玩家的手牌
     */
    sendPlayerCard : function () {
        this._isLookCards = true;

        var list = rules.sortPlayerPokerByNum(this._handCards.getInitCards(), true);

        this.send("onPlayerCard", {
            playerIndex : this.getIndex(),
            cards : list,
            cardValue : this._cardValue
        })

    },

    /**
     * 玩家每轮下注的内容
     * @param bet
     */
     addTurnBet : function (bet) {

        var betContent = {};
        betContent['playerGameTurn' ] = this._playerGameTurn;

        betContent['bet'] = bet;

        this._playerTurnBet.push (betContent);
    },

    /******************************************************************
     *
     ******************************************************************/

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    settlement: function() {
        this._super();
        this._prehandcards = [];
    },

    /******************************************************************
     * 需要重载的接口
     ******************************************************************/

    getInfo : function () {

        var info = this._super();

        info.createTime = this._user.createTime;

        return info;
    },

    /**
     * 断线重连时的个人信息
     */
    getReconnectInfo: function(isSelf) {

        var info = this._super(isSelf);

        if (isSelf && this._isLookCards) {
            info.handCards = this._handCards.getInitCards();
            info.cardValue = this._cardValue;
        }else {
            info.handLength = this._handCards.getInitCards().length;
        }

        info.isLookCards = this._isLookCards;
        info.status = this._status;
        info.score = this._stat.score;
        info.playerBet = this._playerBet;
        info.gameTurn =  this._playerGameTurn;

        info.turnBet = this._playerTurnBet;

        info.isAlwaysGen = this._isAlwaysGen;

        return info;
    },


    onCurRoundFinished : function (){

    },

    /**
     * 初始手牌需要的张数
     * @returns {number}
     */
    getInitHandCardNeedNum: function() {
        return 3;
    }
});